package ay.shadow.demo.controller;

import ay.shadow.core.body.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试接口控制器
 * @author shadow
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/gateway")
public class GateWayDemoController {

    int temp=0;
    @GetMapping("/test/hello")
//    @SentinelResource(value = "/api/gateway/test/hello",blockHandler = "getOrderNoBlockHandler",blockHandlerClass = GateWayDemoController.class)
    public R getHello() {
//        Thread.sleep(10000);

        temp++;
        //模拟刷新三次中有一个失败的情况，来看下Sentinel异常熔断降级情况
        if(temp%3 == 0){

        }
        Map<String,String> map  = new HashMap<>();
        map.put("title1","小王同学的sentinel验证1");
        map.put("title2","小王同学的sentinel验证2");
        return R.ok(map);
//        throw new RuntimeException("Hello World,出现异常");
    }
    @GetMapping("/demo/hello")
    public R getDemo(){
        return R.ok("demo 你好呀");
    }

    public static String getOrderNoBlockHandler(Exception e){
        String msg = "不好意思，前方拥挤，请您稍后再试";
        return msg;
    }

}
